package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbHospitalDao;
import com.woniuxy.daos.TbHospitalManagerDao;
import com.woniuxy.daos.TbUserDao;
import com.woniuxy.daos.TbUserRoleDao;
import com.woniuxy.entitys.TbHospital;
import com.woniuxy.entitys.TbHospitalManager;
import com.woniuxy.entitys.TbUser;
import com.woniuxy.entitys.TbUserRole;
import com.woniuxy.service.HospitalMangerService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

/**
 * @author wu
 * @date 2024/01/11 18:42:19
 */
@Service
public class HospitalMangerServiceImpl implements HospitalMangerService {
    @Autowired
    private TbHospitalManagerDao tbHospitalManagerDao;
    @Autowired
    private TbHospitalDao tbHospitalDao;
    @Autowired
    private TbUserDao tbUserDao;
    @Autowired
    private TbUserRoleDao tbUserRoleDao;
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<TbHospitalManager> getAllHospManger(String input, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
       List<TbHospitalManager> list= tbHospitalManagerDao.getAllHospManger(input);
        PageInfo<TbHospitalManager> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int deleteMangerById(Integer id,Integer flag) {
        TbHospitalManager tbHospitalManager = tbHospitalManagerDao.selectByPrimaryKey(id);
        Integer hid = tbHospitalManager.getHid();
        TbHospital tbHospital = new TbHospital();
        tbHospital.setId(hid);
        tbHospital.setBeiyong1("0");
        int i = tbHospitalManagerDao.updateMangerById(id,flag);
        tbHospitalDao.updateByPrimaryKeySelective(tbHospital);
        return i;
    }

    @Override
    public int resetPasswordById(Integer id) {
        int i = tbUserDao.resetPasswordById(id);
        return i;
    }

    @Override
    public int saveHospitalMer(String phone) {
        TbUser tbUser = new TbUser();
        tbUser.setPhone(phone);
        tbUser.setUpwd("123");
        int count=  tbUserDao.saveHospitalMer(tbUser);
        Integer uid = tbUser.getUid();
        TbUserRole tbUserRole = new TbUserRole();
        tbUserRole.setUid(uid);
        tbUserRole.setRid(7);
        tbUserRoleDao.insert(tbUserRole);
        TbHospitalManager tbHospitalManager = new TbHospitalManager();
        tbHospitalManager.setSoftdel(1);
        tbHospitalManager.setUid(uid);
        tbHospitalManagerDao.insert(tbHospitalManager);
        return count;
    }

    @Override
    public TbHospitalManager loadWorkerInfo() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token  = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:"+username);
        Integer uid = loginUser.getUser().getUid();
        TbHospitalManager tbHospitalManager =tbHospitalManagerDao.getHidAndDidByUid(uid);
        return tbHospitalManager;
    }

    @Override
    public int updHospMangerInfos(TbHospitalManager tbHospitalManager) {
        int cou =tbHospitalManagerDao.updHospMangerInfos(tbHospitalManager);
        return cou;
    }
}
